package HomeWork;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 斗地主发牌器
 */

public class DouDiZhu {
    ArrayList<Poker> pokers = new ArrayList<>();

    /**
     * 初始化54张扑克牌
     */
    public void init() {
        pokers.add(new Poker("", "大王👑", 17,0));
        pokers.add(new Poker("", "小王👑", 16,0));

        int num;
        String point;
        String color;
        for (int i = 3; i < 16; i++) {
            num = i;
            point = i + "";
            if (i == 11) {
                point = "J";
            } else if (i == 12) {
                point = "Q";
            } else if (i == 13) {
                point = "K";
            } else if (i == 14) {
                point = "A";
            } else if (i == 15) {
                point = "2";
            }
            for (int j = 0; j < 4; j++) {
                color = null;
                int suit = 0;
                if (j == 0) {
                    color = "♠";
                    suit = 4;
                } else if (j == 1) {
                    color = "♥";
                    suit = 3;
                } else if (j == 2) {
                    color = "♣";
                    suit = 2;
                } else if (j == 3) {
                    color = "♦";
                    suit = 1;
                }
                pokers.add(new Poker(point, color, num, suit));

            }
        }
        System.out.println(pokers);

    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(pokers);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        List player01 = pokers.subList(0,17);
        List player02 = pokers.subList(17,34);
        List player03 = pokers.subList(34,51);
        List landlord = pokers.subList(51,54);

        sort(player01);
        sort(player02);
        sort(player03);


        System.out.println("玩家01：");
        System.out.println(player01);
        System.out.println("玩家02：");
        System.out.println(player02);
        System.out.println("玩家03：");
        System.out.println(player03);
        System.out.println("底牌：");
        System.out.println(landlord);

        ArrayList<Poker> Dizhupi = new ArrayList<>();
        Dizhupi.add(pokers.get(51));
        Dizhupi.add(pokers.get(52));
        Dizhupi.add(pokers.get(53));
        pokers = new ArrayList<>();
        for (Object obj : player01){
            pokers.add((Poker) obj);
        }
        for (Object obj : player02){
            pokers.add((Poker)obj);
        }
        for (Object obj : player03){
            pokers.add((Poker)obj);
        }
        for (Poker obj : Dizhupi){
            pokers.add(obj);
        }
    }
    private void sort(List list) {
        Collections.sort(list, new Comparator<Poker>() {
            @Override
            public int compare(Poker o1, Poker o2) {
                if (o1.getNum() == o2.getNum()){
                    return o2.getSuit() - o1.getSuit();
                }else {
                    return o1.getNum() - o2.getNum();
                }
            }
        });
    }
}
